<?php

/*
  Project     : 48c6c450f1a4a0cc53d9585dc0fee742
  Created on  : Mar 16, 2013, 11:29:15 PM
  Author      : Truong Khuong - khuongxuantruong@gmail.com
  Description :
  Purpose of the stylesheet follows.
 */

class user_model extends Grid_Model {
    private $table;
    function __construct()
    {
        parent::__construct();
        $this->table="auth_users";
    }
    function get($_id){
        $query=$this->db
                ->where("ause_id",$_id)
                ->get("auth_users");
        $error_number = $this->db->_error_number();
        $error_message = $this->db->_error_message();
        if($error_number!==0){
            //baotri("$error_message");
            return null;
        }
        $rs = $query->result();
        if(isset($rs[0]))   {
            return $rs[0];
        }
        return null;
    }
    function gets(){
        $query=$this->db
                ->where('ause_delete',null)
                ->order_by('ause_position','DESC')
                ->order_by('ause_insert','DESC')
                ->get('auth_users');
        $error_number = $this->db->_error_number();
        $error_message = $this->db->_error_message();
        if($error_number!==0){
            $_SESSION['auth_db']['errors'][]=$error_message;
            return null;
        }
        $rs = $query->result();
        return $rs;
    }
    function insert($params){
        //$this->db->set('_insert', 'NOW()', FALSE);
        @$this->db->insert("auth_users", $params); 
		$error_number = $this->db->_error_number();
        $error_message = $this->db->_error_message();
        if($error_number!==0){
            $_SESSION['auth_db']['errors'][]=$error_message;
            return false;
        }
        @$count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function delete($ID){
        $this->db->set('ause_delete', 'NOW()', FALSE);
        $where=array('ause_id'=>$ID);
        $this->db->where($where);
        $this->db->update("auth_users"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function retore($ID){
        $this->db->set('ause_delete', 'NULL', FALSE);
        $where=array('ause_id'=>$ID);
        $this->db->where($where);
        $this->db->update("auth_users"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($ID,$params){
        $this->db->set('ause_update', 'NOW()', FALSE);
        $this->db->where('ause_id', $ID);
        @$this->db->update("auth_users", $params); 
		$error_number = $this->db->_error_number();
        $error_message = $this->db->_error_message();
        if($error_number!==0){
            $_SESSION['auth_db']['errors'][]=$error_message;
            return false;
        }
        @$count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function getprivileges($_userid){
        $query=$this->db
                ->select("`apri_id`,`apri_name`,`apri_key`,`apri_position`,aupr_user,aupr_permission")
                ->from("auth_privileges")
                ->join(
                        "auth_user_privilege",
                        'aupr_privilege=apri_id AND aupr_status=\'true\' AND aupr_user=\''.$_userid.'\'',
                        "left")
                ->where(array(
                    //"`auth_user_privilege`.`_user`"=>$_userid,
                    "`apri_status`"=>"true"
                ))
                ->get();
        $error_number = $this->db->_error_number();
        $error_message = $this->db->_error_message();
        if($error_number!==0){
            $_SESSION['auth_db']['errors'][]=$error_message;
            return null;
        }
        $rs = $query->result();
        return $rs;
    }
    function insert_onduplicate_update_aupr($aParamsi,$aUpdate){
        $this->db->on_duplicate_update('auth_user_privilege', $aUpdate, $aParamsi); 
        $count = $this->db->affected_rows();
		$error_number = $this->db->_error_number();
        $error_message = $this->db->_error_message();
        if($error_number!==0){
            $_SESSION['auth_db']['errors'][]=$error_message;
            return false;
        }
        if($count>0) return $count;
        return false;
    }
    function binding(){
        $position=isset($_SESSION["auth"]["user"])?$_SESSION["auth"]["user"]->ause_position:0;
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `auth_users`.*,`auth_groups`.`agrp_name`,ause_id as _id
            FROM `auth_users`
                LEFT JOIN `auth_groups` ON (`ause_group` = `agrp_id`)
            ";
        $strWhere="
            WHERE  `ause_position`<=$position
            ";
        if($_SESSION["ADP"][CCTRL]["display"]===0){
            $strWhere.=" AND `ause_delete` IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]===-1){
            $strWhere.=" AND `ause_delete` IS NOT NULL";
        }
        $strOrderBy="Order By `ause_insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>false,
            "fields"=>array(
                
            ),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
    function colmod(){
        return array(
            array(  
                "text"          =>"ID"            
                ,"datafield"    =>"_id" 
                ,"cellsalign"   =>"right"
                ,"width"        => 80 
                ,"hidden"       =>true
                ),
            array(  
                "text"          =>"Name"            
                ,"datafield"    =>"ause_name" 
                //,"width"        => 220
                ,"editable"     =>false
                ),
            array(  
                "text"          =>"Username"            
                ,"datafield"    =>"ause_username" 
                ,"width"        => 220
                ,"editable"     =>false
                ,"hidden"       =>true
                ),
            array(  
                "text"          =>"Email"            
                ,"datafield"    =>"ause_email" 
                ,"width"        => 220
                ,"editable"     =>false
                ,"hidden"       =>true
                ),
            array(  
                "text"          =>"Group"            
                ,"datafield"    =>"agrp_name" 
                ,"columntype"   =>"dropdownlist"
                ,"filtertype"   =>"checkedlist"  
                ,"filteritems"  => array("Admin","Administrator","User","Customer","Developer","Partner")
                ,"width"        => 120 
                ,"editable"     =>false
                ),
            array(  
                "text"          =>"Active"            
                ,"datafield"    =>"ause_status" 
                ,"type"         =>"bool"
                ,"width"        => 100   
                ,"columntype"   =>"checkbox"
                ,"threestatecheckbox"=> false
                ,"filtertype"   =>"bool"  
                ,"editable"     =>false
                ,"filterable"   =>false
                //,"hidden"       =>true
                ),
            array(  
                "text"          =>"Insert"            
                ,"datafield"    =>"ause_insert" 
                ,"width"        => 120    
                ,"cellsformat"  =>'yyyy-MM-dd HH:mm:ss'
                ,"hidden"       =>true
                ),
            array(  
                "text"          =>"Update"            
                ,"datafield"    =>"ause_update" 
                ,"width"        => 120    
                ,"cellsformat"  =>'yyyy-MM-dd HH:mm:ss'
                ,"hidden"       =>true
                ),
            array(  
                "text"          =>"Delete"            
                ,"datafield"    =>"ause_delete" 
                ,"width"        => 120    
                ,"cellsformat"  =>'yyyy-MM-dd HH:mm:ss'
                ,"hidden"       =>true
                )
        );
    }
}

?>
